package com.stb.llh.service;

import com.alibaba.fastjson.JSON;
import com.stb.llh.dao.UserDao;
import com.stb.llh.entity.User;
import com.stb.llh.util.TimesGenerator;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author Lee
 * @Date 2021/11/1
 * @Description
 */

@Repository
@Transactional
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RestHighLevelClient restHighLevelClient;



    public User checkUserandPwd(String username,String pwd){
        return userDao.findUserByUsernameAndPwd(username,pwd);
    }

    public User checkUser(String username){
        return userDao.findUserByUsername(username);
    }

    public boolean insertUser(String username,String pwd){
        User save = userDao.save(new User(username, pwd, TimesGenerator.time()));
        return save != null;
    }

    public boolean insertData() throws IOException {

        Map<String,String> list = new HashMap<>();

        for (int i = 0; i < 100; i++) {
            list.put(String.valueOf(i),UUID.randomUUID().toString());
        }

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("2m");

        for (Map.Entry<String,String> e : list.entrySet()) {
            System.out.println(JSON.toJSONString(e));
            bulkRequest.add(new IndexRequest("s")
            .source(JSON.toJSONString(e), XContentType.JSON));

        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

        return !bulk.hasFailures();
    }

//    获取数据
    public List<Map<String, Object>> searchPage(String key) throws IOException {

//        条件搜索
        SearchRequest searchRequest = new SearchRequest("s");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//        分页 暂时没有

//        精准匹配
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", key);
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

//        执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

//        解析结果
        List<Map<String, Object>> res = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            res.add(sourceAsMap);


        }
        return res;


    }






}
